<?php
namespace App\Service\OrderService\OrderInterfaceService;
use Service\BaseService;
use Service\Code\Normal;
use App\Model\PssModel\OrderRefund;
use App\Model\PssModel\OrderSupplier;
use App\Service\OrderService\OrderRefundService\OrderRefundMessage;
use App\Service\OrderService\OrderBdCrmTradeService;
use App\Model\PssModel\OrderRefundGoods;
use App\Model\PssModel\OrderSupplierGoods;

/**
 * 退款审核时候的查询，是否需要钱买分抵扣
 */
class OrderAfterMoneyToBuyPointsQuery extends BaseService
{
    /**
     * 付款审核
     */
    public function index( array $param = [] )
    {
        $binfo  = $param['binfo'];
        $bid    = $binfo['bid'];
        //当前换货申请，仅支持自营BBC业务
        if( $binfo['is_channel'] != 1 ) {
            return Normal::returnCode(Normal::FAIL,"当前付款申请，仅支持自营BBC业务");
        }
        
        $requestData = $param['requestData'];
        
        if( empty($requestData['refundID']) ) {
            return Normal::returnCode(Normal::FAIL,"售后订单refundID不能为空");
        }
        
        $refundID = $requestData['refundID'];
        $orderRefund = OrderRefund::instance()->info(['bid'=>$bid,'id'=>$refundID]);
        if( empty($orderRefund)) {
            return Normal::returnCode(Normal::FAIL,"售后申请订单记录不存在，请联系管理员进行核实处理");
        }
        
        $orderRefundGoods = OrderRefundGoods::instance()->getRefundGoods($refundID);
        if( empty($orderRefundGoods)) {
            return Normal::returnCode(Normal::FAIL,"售后订单商品数据不存在，请联系管理员进行核实处理");
        }
        
        //判断是否是云仓应该审核的退款单
        if( $orderRefund['refund_source'] == OrderRefundMessage::refund_source_channel ) {
            return Normal::returnCode(Normal::FAIL,"当前云仓不能审核BBC自己的退款单，请联系管理员进行核实处理");
        }
        
        $refund_status      = $orderRefund['refund_status'];
        $order_refund_id    = $orderRefund['id'];
                
        //原始订单信息
        $supplierOrder = OrderSupplier::instance()->info(['id'=>$orderRefund['sub_order_id']]);
        if( empty($supplierOrder)) {
            return Normal::returnCode(Normal::FAIL,"原始供应商订单数据不存在，请联系管理员进行核实处理");
        }
        
        $orderGoods = OrderSupplierGoods::instance()->info(['order_id'=>$supplierOrder['id']]);
        if( empty($orderGoods)) {
            return Normal::returnCode(Normal::FAIL,"原始供应商订单商品数据不存在，请联系管理员进行核实处理");
        }
        
        //做幂等返回处理
        $refund_bd_crm_contents = @json_decode($orderRefund['refund_bd_crm_contents'],true);
        if( ! empty($refund_bd_crm_contents['serverBillIdPreConfirmPay']) && $refund_bd_crm_contents['serverBillIdPreConfirmPay'] == true ) 
        {
            $data = [
                'needBuyCentMoney' => $orderRefund['refund_buy_points_money'],
            ];
            return Normal::returnCode(Normal::SUC,"获取钱买分抵扣查询成功",$data);
        }
        
        //通知百大CRM交易售后接口
        $refund_bd_crm_param = @json_decode($orderRefund['refund_bd_crm_create'],true);
        $order  = [
            'refundType'            => $orderRefund['refund_type'],
            'refund_extra_value'    => [
                'vipId' => $refund_bd_crm_param['requestData']['vipId'],
                'type'  => $refund_bd_crm_param['requestData']['type'],
            ]
        ];
        $refundGoodsList = [];
        $result = OrderBdCrmTradeService::instance()->uploadBdCrmOrderAfterSale(
            $binfo, 
            $orderRefund,
            $orderRefundGoods,
            $supplierOrder,
            $order 
        );
        
        //如果钱买分返回成功
        if( $result['code'] == Normal::SUC && 
            isset($result['data']['needBuyCentMoney']) && 
            $result['data']['needBuyCent'] == true &&
            $result['data']['needBuyCentMoney'] > 0 ) 
        {
            $update_arr = [
                'refund_buy_points_money' => $result['data']['needBuyCentMoney'],
                'refund_extra_value'    => json_encode([
                    'vipId'     => $refund_bd_crm_param['requestData']['vipId'],
                    'money'     => $result['data']['needBuyCentMoney'],
                ],JSON_UNESCAPED_UNICODE),
                'refund_extra_mode'     => '1',
            ];
            OrderRefund::instance()->where('id',$refundID)->update($update_arr);
        }
        return $result;
    }
}